﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;
using OA.Core.Model;
using OA.Model;
using OA.Model.DB;
using OA.Web.Areas.Member.Controllers;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using OA.Core.EF;
using X.PagedList;

namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class KnowledgeController : BaseMemberController
    {
        private IMemoryCache _cache;
        public KnowledgeController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }



        [MyActionFilter("206002", "知识管理", "管理中心",true, "iconzhishi")]
        public IActionResult Index()
        {
            List<SimpleKnowledgeGroup> groups = this.GetAll<KnowledgeGroup, SimpleKnowledgeGroup>(m => m.Status != -1, SimpleKnowledgeGroup.Map);
            ViewBag.KnowledgeGroup = groups;
            return View();
        }

        [MyActionFilter("206002", "知识管理")]
        public IActionResult GetData(string keyword, int typeID = 0, int page = 1, int limit = 20)
        {
            ViewBag.TypeID = typeID;
            Expression<Func<VKnowledge, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = where.AndAlso<VKnowledge>(m => m.Title.Contains(keyword));
            }
            if (typeID > 0)
            {
                where = where.AndAlso<VKnowledge>(m => m.GroupID == typeID);
            }
            var list = this.dbContextFactory.DbContext.VKnowledge.Where(where).OrderByDescending(m => m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.TotalItemCount,
                data = list
            };
            return Json(result);
        }

        [HttpGet]
        [MyActionFilter("206002001", "添加知识", "知识管理", false, "")]
        public IActionResult Create()
        {
            var list = this.dbContextFactory.DbContext.Database.SqlQuery<KnowledgeGroup>("select * from  KnowledgeGroup").ToList();

            List<ListItem> selectList = list.Select(m => new ListItem()
            {
                Text = m.Name,
                Value = m.ID.ToString()
            }).ToList();
            ViewData["selectList"] = selectList;
            return View(new Knowledge() { });
        }




        [HttpPost]
        [MyActionFilter("206002001", "添加知识", "知识管理", false, "")]
        public IActionResult Create(Knowledge model)
        {
            BaseResult baseResult = new BaseResult();
            try
            {
                if (!ModelState.IsValid)
                {
                    baseResult.State = 400;
                    baseResult.Msg = this.GetModelStateError(ModelState);
                    return Json(baseResult);
                }
                else
                {
                    this.AddInfo<Knowledge>(model);
                    baseResult.Msg = "添加知识成功";
                    return Json(baseResult);

                }
            }
            catch(Exception ex)
            {
                baseResult.State = 400;
                baseResult.Msg = ex.Message;
                return Json(baseResult);
            }
            

        }

        [HttpGet]
        [MyActionFilter("206002002", "编辑知识", "知识管理", false, "")]
        public IActionResult Edit(int id)
        {
            Knowledge infoKnowledge = this.GetInfo<Knowledge>(id);
            if (infoKnowledge == null)
            {
                return Redirect("/Management/Knowledge?msg=沒有找到该知识");
            }
            var list = this.dbContextFactory.DbContext.Database.SqlQuery<KnowledgeGroup>("select * from  KnowledgeGroup").ToList();

            List<ListItem> selectList = list.Select(m => new ListItem()
            {
                Text = m.Name,
                Value = m.ID.ToString()
            }).ToList();
            ViewData["selectList"] = selectList;
            return View(infoKnowledge);
        }

        [HttpPost]
        [MyActionFilter("206002002", "编辑知识", "知识管理", false, "")]
        public IActionResult Edit(int id, Knowledge model)
        {
            BaseResult baseResult = new BaseResult();
            try
            {
                if (!ModelState.IsValid)
                {
                    baseResult.State = 400;
                    baseResult.Msg = this.GetModelStateError(ModelState);
                    return Json(baseResult);
                }
                Knowledge infoPageGroup = this.GetInfo<Knowledge>(id);
                if (infoPageGroup == null)
                {
                    baseResult.State = 400;
                    baseResult.Msg = "沒有找到该知识";
                    return Json(baseResult);
                }
                this.Update<Knowledge>(infoPageGroup, model, new string[] { "Title", "Content", "IsTop", "Author" });
                baseResult.Msg = "知识更新成功!";
                return Json(baseResult);
            }
            catch(Exception ex)
            {
                baseResult.State = 400;
                baseResult.Msg = ex.Message;
                return Json(baseResult);
            }
        }


        [HttpGet]
        [MyActionFilter("206002003", "删除知识", "知识管理", false, "")]
        public IActionResult Delete(int id)
        {
            Expression<Func<Knowledge, bool>> where = m => m.ID == id;
            this.Delete<Knowledge>(where);
            return Json(new { State = 200, Msg = "删除知识成功" });
        }
        
    }
}
